/*
 * Copyright (c) 2020-2099 the original author or authors. All rights reserve.
 */
package org.tianyun.cloud.amqp.rabbitmq.message;

import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.core.MessageProperties;
import org.tianyun.cloud.context.RequestContext.RequestContextHolder;
import org.tianyun.cloud.context.ServletContext.ContextOperator;
import org.tianyun.cloud.context.ServletContext.ServletContextHolder;

/**
 * 发送前消息处理
 *
 * @auther ebert_chan
 */
public class BeforePublishPostProcessor implements MessagePostProcessor {

    /*
     * @see org.springframework.amqp.core.MessagePostProcessor#postProcessMessage(org.springframework.amqp.core.Message)
     */
    @Override
    public Message postProcessMessage(Message message) throws AmqpException {
        MessageProperties messageProperties = message.getMessageProperties();

        ContextOperator operator = (key, value) -> {
            messageProperties.setHeader(key, value);
        };
        RequestContextHolder.dispatch(operator);
        ServletContextHolder.dispatch(operator);

        return message;
    }

}
